package hotel.dao;

import java.util.List;

import javax.management.Query;

import hotel.tipos.Conta;
import hotel.tipos.ItemConta;
import hotel.util.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class ContaDao {
	public static int create(Conta conta){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				
				session.beginTransaction();

		        Integer id = (Integer) session.save(conta);
		        
		        session.getTransaction().commit();
		        
		        return id.intValue();
			}
	
	@SuppressWarnings("unchecked")
	public static List<ItemConta> listarServicos(Conta c) {
		// Cria uma sessão
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		// inicia a transação
		session.beginTransaction();
		// Cria uma lista para retornar
		List<ItemConta> itens = null;
		
		try{
			/*Criteria select = session.createCriteria(Conta.class);
			select.setFetchMode("itensConta", FetchMode.JOIN);
			select.add(Restrictions.eq("contaId", new Integer(c.getContaId())));
			*/
			String query = new String("select c.itens from Conta c where c.contaId = :id");
			org.hibernate.Query q = session.createQuery(query);
			q.setInteger("id", c.getContaId());
			itens = q.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return itens;
	}
}
